package com.lun.blog.controller.admin;

import com.lun.blog.pojo.Type;
import com.lun.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 跳转标签显示页面（分页展示）
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping("/redirectType")
    public String redirectType(@PageableDefault(size = 5,sort = "id",direction = Sort.Direction.ASC) Pageable pageable,
                               Model model
    ){
        model.addAttribute("page",typeService.listPage(pageable));
        return "admin/types";
    }

    /**
     * 跳转到新增标签页面
     * @return
     */
    @RequestMapping("/addTypePage")
    public String addTypePage(){
        return "admin/types-input";
    }

    /**
     * 新增加标签
     * @param type 获取标签名称
     * @param redirectAttributes
     * @return
     * @throws Exception
     */
    @PostMapping("/saveType")
    public String saveType(Type type, RedirectAttributes redirectAttributes) throws Exception {
        Boolean isExist = typeService.findByTypeName(type.getName());
        if (isExist) {
            //todo 标签不存在，数据库加入
            Type type1 = typeService.saveType(type);
            redirectAttributes.addFlashAttribute("message", "保存" + type1.getName() + "成功");
        }else {
            //todo 标签存在，不做跳转，直接在当前页面提示信息
            redirectAttributes.addFlashAttribute("message", "保存失败," + type.getName() + "已存在");
        }
        return "redirect:/admin/redirectType";
    }

    /**
     * 跳转到修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toUpdatePage")
    public String toUpdatePage( Long id, Model model){
        Type type = typeService.getType(id);
        model.addAttribute("oldType",type);
        return "admin/types-update";
    }

    /**
     * 修改type
     * @param id
     * @param name
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/updateType")
    public String updateType(String id,String name,RedirectAttributes redirectAttributes){
        //todo 先判断数据库中是否存在同名
        List<Type> types = typeService.findAll();
        for (Type type : types) {
            if(type.getName().equals(name)){
                redirectAttributes.addFlashAttribute("message","标签已存在，请重新操作");
                return "redirect:/admin/redirectType";
            }
        }
        //todo 未测试 修改type
        long typeId = Long.parseLong(id);
        Type type = typeService.getType(typeId);
        String oldName = type.getName();
        type.setName(name);
        Type returnType = typeService.saveType(type);
        if(returnType==null){
            redirectAttributes.addFlashAttribute("message","修改失败，请重新尝试");
        }else {
            redirectAttributes.addFlashAttribute("message","你已经将"+oldName+"修改为"+name);

        }
        return "redirect:/admin/redirectType";
    }

    /**
     * 删除一个标签，返回删除成功信息
     * @param id
     * @param
     * @return
     */
    @GetMapping("/deleteType")
    public String deleteType(Long id,RedirectAttributes redirectAttributes){
        typeService.deleteType(id);
        redirectAttributes.addFlashAttribute("message","删除标签成功");
        return "redirect:/admin/redirectType";
    }
}
